let mysql = require("mysql");
let pool = mysql.createPool({
  connectionLimit: 10, //连接数为10
  host: "47.100.55.117",
  user: "root",
  password: "TY911221",
  database: "test"
  //   database: "video_yk"
});

export let db: Function = (sqlstr: String, insertParams: String) => {
  return new Promise((res, rej) => {
    pool.getConnection((err, connection) => {
      console.log("数据库连接开始...");
      if (err) {
        console.log("数据库报错", err);
      } else {
        connection.query(sqlstr, insertParams, (err, result) => {
          if (err) {
            console.log(err);
            rej(err);
          } else {
            console.log(result);
            res(result);
          }
        });

        connection.release(); //释放连接
        console.log("释放连接...");
      }
    });
    //打开连接 日志处理
    pool.on("acquire", function(connection) {
      console.log("打开连接", connection.threadId);
    });
    // 连接队列
    pool.on("enqueue", function() {
      console.log("连接队列");
    });

    // //此处关闭连接池需逻辑调用
    // pool.end(function(err) {
    //   // all connections in the pool have ended
    //   console.log("数据可被关闭", err);
    // });
  });
};
// export = db;
